//
//  GQHDriverAuthorizationView.m
//  Xanthium
//
//  Created by GuanQinghao on 31/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHDriverAuthorizationView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHDriverAuthorizationView () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    
    NSArray *titleArray;
    NSArray *_palceHolderArray;
    NSArray *_perArray;
}

@end


@implementation GQHDriverAuthorizationView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"* 联系人",@"* 联系电话",@"身份证号码",@"* 从业资格证号码",@"* 所有人",@"* 道路运输许可证号",@"* 道路经营许可证号",@"* 车牌号",@"挂车牌照号",@"* 车型",@"* 车长",@"* 车牌类型",@"* 载重"];
    _qh_contentArray = [NSMutableArray arrayWithArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"必选",@"必选",@"必选",@""]];
    _palceHolderArray = @[@"必填",@"必填",@"选填",@"必填",@"必填",@"必填",@"必填",@"必填",@"选填",@"必选",@"必选",@"必选",@"必填"];
}

#pragma mark --View
- (void)loadUserInterface
{
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    [identifier stringByAppendingFormat:@"%ld",indexPath.row];
    
    YYEnterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"YYEnterTextCell" owner:self options:nil].firstObject;
    }
    
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.showTitleText = titleArray[indexPath.row];
    if (indexPath.row == 9 || indexPath.row == 10 || indexPath.row == 11 )
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.isEnter = NO;
        cell.showValueLabel.text = _qh_contentArray[indexPath.row];
    }
    else
    {
        cell.isEnter = YES;
        cell.enterText.text = _qh_contentArray[indexPath.row];
        cell.enterText.placeholder = _palceHolderArray[indexPath.row];
        cell.enterText.delegate = self;
        cell.enterText.tag = indexPath.row;
    }
    
    if (indexPath.row == 12)
    {
        cell.perLabel.text = @"吨";
    }
    else
    {
        cell.perLabel_width.constant = 0;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 9 || indexPath.row == 10 || indexPath.row == 11 )
    {
        if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
            
            [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 60.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView = [[UIView alloc] init];
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.backgroundColor = kAppRedColor;
    nextButton.layer.cornerRadius = 5.0f;
    nextButton.layer.masksToBounds = YES;
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:nextButton];
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(footerView).with.inset(10);
        make.left.and.right.mas_equalTo(footerView).with.inset(20);
    }];
    
    return footerView;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [_qh_contentArray replaceObjectAtIndex:textField.tag withObject:textField.text];
}

- (IBAction)next:(UIButton *)sender {
    DLog(@"下一步，上传证照");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_nextPage)]) {
        
        [_qh_delegate qh_nextPage];
    }
}

@end
